package java44th.group.controller.admin.topic;

import java44th.group.bean.admin.bo.topic.TopicBatchDeleteByIdBo;
import java44th.group.bean.admin.bo.topic.TopicCreateBo;
import java44th.group.bean.admin.bo.topic.TopicListBo;
import java44th.group.bean.admin.bo.topic.TopicReadById;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.MarketTopic;
import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.service.admin.topic.TopicService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Aurhor: 帅的一批
 * @CreateAt: 2022/10/13 11:14
 * @Description:
 * @PACKAGE_NAME： java44th.group.controller.admin.topic
 */
@Transactional
@RestController
@RequestMapping("admin/topic")
public class TopicController {

    @Autowired
    TopicService topicService;

    @RequiresPermissions(value={"admin:topic:list","*"},logical= Logical.OR)
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public BaseRespVo getTopicList(TopicListBo topicListBo) {
        CommonData commonData = topicService.getTopicList(topicListBo);
        if (commonData == null) {
            return BaseRespVo.invalidParameter("参数错误");
        }
        return BaseRespVo.ok(commonData);
    }

    @RequiresPermissions(value={"admin:topic:create","*"},logical= Logical.OR)
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public BaseRespVo createTopic(@RequestBody TopicCreateBo topicCreateBo) {
        if (!topicCreateBo.checkDataValid()) {
            return BaseRespVo.invalidData("参数错误");
        }
        MarketTopic marketTopic = topicService.createTopic(topicCreateBo);
        if (marketTopic == null) {
            return BaseRespVo.invalidData("插入失败");
        }
        return BaseRespVo.ok(marketTopic);
    }

    @RequiresPermissions(value={"admin:topic:read","*"},logical= Logical.OR)
    @RequestMapping(value = "read", method = RequestMethod.GET)
    public BaseRespVo getTopicById(Integer id) {
        TopicReadById topicByid = null;
        try {
            topicByid = topicService.getTopicByid(id);
        } catch (Exception e) {
            return  BaseRespVo.invalidParameter("什么玩意?我能报错?");
        } finally {
        }
        return BaseRespVo.ok(topicByid);
    }
    @RequiresPermissions(value={"admin:topic:delete","*"},logical= Logical.OR)
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public BaseRespVo deleteTopic(@RequestBody MarketTopic marketTopic) {

        try {
            topicService.virtualDelete(marketTopic);
        } catch (Exception e) {
            return  BaseRespVo.invalidParameter("什么玩意?我能报错?");
        }

        return BaseRespVo.ok();
    }

    @RequestMapping(value = "batch-delete", method = RequestMethod.POST)
    public BaseRespVo deleteBatchTopicByid(@RequestBody TopicBatchDeleteByIdBo topicBatchDeleteByIdBo) {

        try {
            topicService.virtualDeleteBatchTopicByid(topicBatchDeleteByIdBo);
        } catch (Exception e) {
            return  BaseRespVo.invalidParameter("什么玩意?我能报错?");
        }

        return BaseRespVo.ok();
    }

    @RequiresPermissions(value={"admin:topic:update","*"},logical= Logical.OR)
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public BaseRespVo updateTopic(@RequestBody MarketTopic marketTopic) {

        if (!marketTopic.checkDataValid()) {
            return BaseRespVo.invalidData("参数错误");
        }
        marketTopic = topicService.updateTopic(marketTopic);

        return BaseRespVo.ok(marketTopic);
    }

}
